package io.github.mcsdk.ui.util;

import net.minecraft.client.gui.GuiGraphics;

/**
 * Rendering utility functions for UI components
 *
 * @author MC SDK Team
 * @version 1.0.0
 * @since 1.0.0
 */
public class RenderUtil {

    /**
     * Draws a bordered box
     *
     * @param graphics Graphics context
     * @param x X position
     * @param y Y position
     * @param width Width
     * @param height Height
     * @param bgColor Background color (ARGB)
     * @param borderColor Border color (ARGB)
     */
    public static void drawBorderedBox(GuiGraphics graphics, int x, int y, int width, int height,
                                        int bgColor, int borderColor) {
        // Background
        graphics.fill(x, y, x + width, y + height, bgColor);

        // Border
        graphics.fill(x, y, x + width, y + 1, borderColor); // Top
        graphics.fill(x, y + height - 1, x + width, y + height, borderColor); // Bottom
        graphics.fill(x, y, x + 1, y + height, borderColor); // Left
        graphics.fill(x + width - 1, y, x + width, y + height, borderColor); // Right
    }

    /**
     * Draws a gradient box
     *
     * @param graphics Graphics context
     * @param x X position
     * @param y Y position
     * @param width Width
     * @param height Height
     * @param colorStart Start color (ARGB)
     * @param colorEnd End color (ARGB)
     */
    public static void drawGradientBox(GuiGraphics graphics, int x, int y, int width, int height,
                                        int colorStart, int colorEnd) {
        graphics.fillGradient(x, y, x + width, y + height, colorStart, colorEnd);
    }

    /**
     * Draws a shadow effect
     *
     * @param graphics Graphics context
     * @param x X position
     * @param y Y position
     * @param width Width
     * @param height Height
     * @param shadowOffset Shadow offset
     * @param shadowColor Shadow color (ARGB)
     */
    public static void drawShadow(GuiGraphics graphics, int x, int y, int width, int height,
                                   int shadowOffset, int shadowColor) {
        graphics.fill(x + shadowOffset, y + shadowOffset,
                     x + width + shadowOffset, y + height + shadowOffset, shadowColor);
    }

    /**
     * Calculates a pulsing alpha value for animations
     *
     * @param time Current time in milliseconds
     * @param minAlpha Minimum alpha (0-255)
     * @param maxAlpha Maximum alpha (0-255)
     * @param speed Animation speed multiplier
     * @return Alpha value (0-255)
     */
    public static int getPulsingAlpha(long time, int minAlpha, int maxAlpha, double speed) {
        double value = Math.sin(time * speed * 0.001);
        double normalized = (value + 1.0) / 2.0; // Convert to 0-1 range
        return (int) (minAlpha + (maxAlpha - minAlpha) * normalized);
    }

    /**
     * Creates ARGB color from components
     *
     * @param alpha Alpha (0-255)
     * @param red Red (0-255)
     * @param green Green (0-255)
     * @param blue Blue (0-255)
     * @return ARGB color value
     */
    public static int argb(int alpha, int red, int green, int blue) {
        return (alpha << 24) | (red << 16) | (green << 8) | blue;
    }

    /**
     * Blends two colors
     *
     * @param color1 First color (ARGB)
     * @param color2 Second color (ARGB)
     * @param ratio Blend ratio (0.0 = color1, 1.0 = color2)
     * @return Blended color (ARGB)
     */
    public static int blendColors(int color1, int color2, float ratio) {
        ratio = Math.max(0, Math.min(1, ratio));

        int a1 = (color1 >> 24) & 0xFF;
        int r1 = (color1 >> 16) & 0xFF;
        int g1 = (color1 >> 8) & 0xFF;
        int b1 = color1 & 0xFF;

        int a2 = (color2 >> 24) & 0xFF;
        int r2 = (color2 >> 16) & 0xFF;
        int g2 = (color2 >> 8) & 0xFF;
        int b2 = color2 & 0xFF;

        int a = (int) (a1 + (a2 - a1) * ratio);
        int r = (int) (r1 + (r2 - r1) * ratio);
        int g = (int) (g1 + (g2 - g1) * ratio);
        int b = (int) (b1 + (b2 - b1) * ratio);

        return argb(a, r, g, b);
    }
}